package com.cxh.rmsq.ui.activity.self

import android.os.Bundle
import android.text.TextUtils
import android.widget.LinearLayout
import android.widget.TextView
import androidx.lifecycle.ViewModelProviders
import com.cxh.rmsq.R
import com.cxh.rmsq.db.model.AuthenticationBean
import com.cxh.rmsq.im.IMManager
import com.cxh.rmsq.ui.BaseActivity
import com.cxh.rmsq.ui.widget.SelectableRoundedImageView
import com.cxh.rmsq.utils.RegexUtils
import com.cxh.rmsq.viewmodel.UserInfoViewModel
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ColorUtils
import com.bumptech.glide.Glide
import io.rong.imkit.userinfo.RongUserInfoManager

class AuthenticationSucessActivity : BaseActivity() {

    private val ivBack: LinearLayout by lazy { findViewById<LinearLayout>(R.id.iv_back) }
    private val tvRealName: TextView by lazy { findViewById<TextView>(R.id.tv_real_name) }
    private val tvIdNumber: TextView by lazy { findViewById<TextView>(R.id.tv_id_number) }
    private val tvNickName: TextView by lazy { findViewById<TextView>(R.id.tv_nick_name) }
    private val rivHeader: SelectableRoundedImageView by lazy { findViewById<SelectableRoundedImageView>(R.id.riv_header) }

    private var data = AuthenticationBean()
    private var userInfoViewModel: UserInfoViewModel? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_authentication_sucess)
        findViewById<LinearLayout>(R.id.rl_title).setPadding(0, BarUtils.getStatusBarHeight(), 0, 0)
        BarUtils.setStatusBarColor(this, ColorUtils.getColor(R.color.transparent))
        BarUtils.setStatusBarLightMode(this, true)

        userInfoViewModel =
            ViewModelProviders.of(this).get<UserInfoViewModel>(UserInfoViewModel::class.java)

        data = intent.getSerializableExtra("dataBean") as AuthenticationBean
        tvRealName.text = data.realName
        tvIdNumber.text = RegexUtils.hideMiddle(data.idNumber)


        ivBack.setOnClickListener{
            finish()
        }
    }

    override fun onResume() {
        super.onResume()
        if (!TextUtils.isEmpty(IMManager.getInstance().currentId)) {
            val userInfo = RongUserInfoManager.getInstance()
                .getUserInfo(IMManager.getInstance().currentId)
            tvNickName.text = userInfo?.name
            Glide.with(this).load(userInfo?.portraitUri).into(rivHeader)

//            if (userInfo == null) {
//                userInfoViewModel!!.requestUserInfo(IMManager.getInstance().currentId)
//                userInfoViewModel!!.requestUserSelfInfo(IMManager.getInstance().currentId)
//            }
        }
    }
}